<?php

class MvcAction extends Object
{

  private $controller;
  private $name;
  private $arguments;

  private $isDefault;

  public function __construct($name, $arguments)
  {
    if(empty($name))
      $name = 'Index';


    $this->name = ucfirst ("{$name}Action");
    $this->arguments = $arguments;

  }

  public function Invoke($controller)
  {
    $this->controller = $controller;
    if (method_exists($this->controller, $this->name)) {

      $args = null;
      $params = My::System()->Type($this->controller->Type)->Parameters($this->name);

      if (!empty($params)) {

        if (empty($this->arguments)) {
          throw new Exception("{$this->name} missing arguments");
        }

        foreach ($params as $param) {
          if (isset($this->arguments[$param->getName()])) {
            $args[] = $this->arguments[$param->getName()];
          }
        }

        return $this->controller->{$this->name}(implode(',', $args));
      } else {
        return $this->controller->{$this->name}();
      }
    }

    throw new Exception("{$this->name} action not found");
  }

}